/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package submarines;

import java.util.LinkedList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import submarines.notices.Notice;

/**
 *
 * @author sshiran
 */
public class Status {
    String[][] players;
    Square[][][] boards;
    String[][][] submarines;
    int num;
    transient LinkedList<Notice> notices;
    
    public Status(PlayerManager pManager) {
        num = pManager.removed.size() + pManager.players.size();
        players = new String[num][3];
        boards = new Square[num][][];
        submarines = new String[num][][];
        
    }
    
    public void setStatus(PlayerManager pManager, HttpSession s)
    {
        int i;
        String[] currPlayerString;
        Player currPlayer;
        for (i=0; i<pManager.players.size(); i++)
        {
            currPlayer = pManager.players.get(i);
            currPlayer.readNotices();
            currPlayerString = getPlayerArray(currPlayer);
            players[i] = currPlayerString;
            OnlineHumanPlayer p = pManager.getPlayerBySession(s.getId());
            if (currPlayer.getName().equals(p.getName())) {
                boards[i] = currPlayer.myBoard.gameBoard;
                notices = currPlayer.getNotices(true);
            } else {
                boards[i] = p.getEnemyBoardByName(currPlayer).gameBoard;
            }
            submarines[i] = getSubmarines(currPlayer.myBoard);
        }
        
        int index = 0;
        for (int j=i; j<num; j++)
        {
            currPlayer = pManager.removed.get(index);
            currPlayerString = getPlayerArray(currPlayer);
            players[j] = currPlayerString;
            boards[j] = currPlayer.myBoard.gameBoard;
            submarines[j] = getSubmarines(currPlayer.myBoard);
            index++;
        }
    }

    private String[] getPlayerArray(Player player) {
        String[] playerString = new String[3];
        playerString[0] = player.getName(); 
        playerString[1] = player.getType().toString();
        playerString[2] = Config.DEATHCAUSE[player.deathCause];
        return playerString;
    }
    
    private String[][] getSubmarines(Board playerBoard)
    {
      String[][] subString = Config.cloneAliases();
        int[] remainingSubs = playerBoard.getRemainingSubsGame().clone();
        int[] initial = Config.SUBMARINES.clone();
        
        for (int i = 0; i <playerBoard.subs.length; i++) {
            for (int j = remainingSubs[i]; j < initial[i]; j++) {
                subString[i][j] = ""; //clear sub from list if player hit is alredy
            }
        }
        return subString;
    }
}

